package services

import (
	"iris_product/datamodels"
	"iris_product/repositories"
)

type IProductService interface {
	GetProductById(id int64) (product *datamodels.Product, err error)
	GetAllProduct()(productList []*datamodels.Product,err error)
	DeleteProductById(id int64) bool
	InsertProduct(product *datamodels.Product)(id int64,err error)
	UpdateProduct(product *datamodels.Product)(err error)
	SubProductNum(id int64) (err error)
}

type ProductService struct {
	productRepo repositories.IProduct
}

func (p *ProductService) GetProductById(id int64) (product *datamodels.Product, err error) {
	product, err = p.productRepo.SelectById(id)
	return
}

func (p *ProductService) GetAllProduct() (productList []*datamodels.Product, err error) {
	productList,err = p.productRepo.SelectAll()
	return
}

func (p *ProductService) DeleteProductById(id int64) bool {
	return p.productRepo.Delete(id)
}

func (p *ProductService) InsertProduct(product *datamodels.Product) (id int64, err error) {
	id,err = p.productRepo.Insert(product)
	return
}

func (p *ProductService) UpdateProduct(product *datamodels.Product) (err error) {
	err = p.productRepo.Update(product)
	return
}

func (p *ProductService) SubProductNum(id int64) (err error) {
	err = p.productRepo.SubProductNumOne(id)
	return
}

func NewProductService(repo repositories.IProduct) IProductService {
	return &ProductService{
		productRepo: repo,
	}
}
